﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Torch_Services
{
    public class SubjectPermissionService : ISubjectPermissionService
    {
        private ISubjectPermissionDal _subjectpermission;
        private IMapper _mapper;
        public SubjectPermissionService(ISubjectPermissionDal subjectpermission, IMapper mapper)
        {
            _subjectpermission = subjectpermission;
            _mapper = mapper;
        }

        public async Task<int> AddSubjectPermissionAsync(int subjectId, int staffId)
        {
            return await _subjectpermission.AddAsync(new Subjectpermission 
            {
                SubjectId = subjectId,
                StaffId = staffId
            });
        }

        public async Task<int> DeleteSubjectPermissionAsync(int id)
        {
            var data = await _subjectpermission.QueryAsync(id);
            return data == null ? -1 : await _subjectpermission.DeleteAsync(data);
        }

        public async Task<int> EditSubjectPermissionAsync(int id, int subjectId, int staffId)
        {
            var data = await _subjectpermission.QueryAsync(id);
            if (data == null)
                return -1;
            data.SubjectId = subjectId;
            data.StaffId = staffId;
            return await _subjectpermission.EditAsync(data);
        }

        public async Task<List<SubjectPermissionDto>> QuerySubjectPermissionAllAsync()
        {
            var data = await _subjectpermission.QueryAsync();
            return _mapper.Map<List<Subjectpermission>,List<SubjectPermissionDto>>(data);
        }

        public async Task<SubjectPermissionDto> QuerySubjectPermissionByIdAsync(int id)
        {
            var data = await _subjectpermission.QueryAsync(id);
            return _mapper.Map<Subjectpermission,SubjectPermissionDto>(data);
        }
    }
}
